//
//  JSTPictureView.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/16.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTPictureView.h"

@interface JSTPictureView ()<UIScrollViewDelegate>

{
    UIScrollView *imageScrollView;
}
@property (nonatomic, strong)NSArray *imageUrls;

@property (nonatomic, strong)UIPageControl *pageControl;

@property (nonatomic, assign)NSInteger currePage;   //当前页


@end

@implementation JSTPictureView

-(id)initWithFrame:(CGRect)frame andOwner:(id)owner
{
    self = [super initWithFrame:frame andOwner:owner];
    
    if (self) {
        JSTPictureViewController *pvc = owner;
        self.imageUrls=pvc.imageUrls;
        self.currePage=pvc.page;
        [self createUI];
    }
    return self;
}
#pragma mark ===============createUI======================
-(void)createUI
{
    imageScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight)];
    //设置代理
    imageScrollView.delegate=self;
    
    
    imageScrollView.pagingEnabled=YES;
    imageScrollView.showsHorizontalScrollIndicator=NO;
    //改变偏移量
    CGPoint point=imageScrollView.contentOffset;
    point.x=(_currePage-1)*kDeviceWidth;
    imageScrollView.contentOffset=point;
    
    imageScrollView.bounces=NO;
    imageScrollView.delegate=self;
    imageScrollView.contentSize=CGSizeMake(_imageUrls.count*kDeviceWidth, 0);
    UIView *v=[[UIView alloc] initWithFrame:CGRectMake(20, 50, 100, 100)];
    v.backgroundColor=[UIColor blackColor];
    [imageScrollView addSubview:v];
    [self addSubview:imageScrollView];
    [_imageUrls enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIScrollView *smallScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(idx*kDeviceWidth, 0, kDeviceWidth, kDeviceHeight)];
        smallScrollView.delegate=self;
        smallScrollView.backgroundColor=[UIColor blackColor];
        smallScrollView.showsHorizontalScrollIndicator=NO;
        smallScrollView.showsVerticalScrollIndicator=NO;
        smallScrollView.maximumZoomScale=2.0;
        smallScrollView.minimumZoomScale=1.0;
        [imageScrollView addSubview:smallScrollView];
        NSString *url=obj;
        // UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(idx*kDeviceWidth, 0, kDeviceWidth, kDeviceHeight)];
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight)];
        //绑定tag值
        imageView.tag=idx;
        
        imageView.backgroundColor=[UIColor blackColor];
        //打开交互
        imageView.userInteractionEnabled=YES;
        imageView.contentMode=UIViewContentModeScaleAspectFit;
        
        [imageView setImageWithURL:[NSURL URLWithString:url]];
        
        //添加手势
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [imageView addGestureRecognizer:tap];
        // [imageScrollView addSubview:imageView];
        [smallScrollView addSubview:imageView];
        
        
    }];
    
    
    _pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(100, kDeviceHeight-40, kDeviceWidth-200, 20)];
    
    _pageControl.numberOfPages=_imageUrls.count;
    
    _pageControl.currentPage=_currePage-1;
    
    _pageControl.pageIndicatorTintColor=[UIColor lightGrayColor];
    [self addSubview:_pageControl];
    
}
#pragma mark ==============点击事件=====================
-(void)tapClick:(UIGestureRecognizer *)tap
{
    
    if (_delegate&&[_delegate respondsToSelector:@selector(imageViewTap)]) {
        [_delegate imageViewTap];
    }
}

#pragma mark UIscrollView代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint point=scrollView.contentOffset;
    int page=(int)point.x/kDeviceWidth;
    _pageControl.currentPage=page;
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if (scrollView==imageScrollView) {
        return nil;
    } else {
        UIImageView *picImageView=[scrollView subviews][0];
        return picImageView;
    }
}
@end
